namespace Vanilla

import System.Reflection
import System.Xml
import System

class Tool:
	//static def OpenDLL (f as string, t as string) as System.Type:
	//	pass

	static private def OpenXML (f as string) as System.Type:
		_d = XmlDocument()
		_d.Load(f)
		d = _d.DocumentElement
		
		s = d.Item["execute"]
		
		dll = s.GetAttribute("dll")
		cl = s.GetAttribute("class")
		
		ass as Assembly
		
		try:
			ass = Assembly.LoadFile(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(f), dll))
		except e:
			ass = Assembly.LoadFile(dll)
		
		return ass.GetType(cl) // BAHAHAHAHAHAHA
	
	// Runs a tool from a tool folder
	static private def OpenTool (f as string) as System.Type:
		return OpenXML(System.IO.Path.Combine(f, "Tool.xml"))
	
	/*
	// Opens the main program window or something - no inputs are given
	abstract def Open ():
		pass
	
	// Opens the given document
	virtual def Open (f as string):
		Open()
	*/
	
	// Execute tool at given path
	static def LoadTool (f as File) as Tool:
		print "LoadTool(${f.Path})"
		t = OpenTool(f.Path)
		print "t=${t}"
		c = t.GetConstructor((File,))
		print "c=${c}"
		if c == null:
			c = t.GetConstructor(array(Type,0))
			print "oops, c=${c}"
			return c.Invoke(array(object,0))
		else:
			return c.Invoke((f,))
